setwd("~/PRQ_contribution_access")
library(tidyverse)
library(rms)
library(stargazer)

#### Appendix D.2 (Organizing the Data by Congress) ####
rm(list = ls())
load("legislator_registrant_congress_data.RData")
load("legislator_registrant_congress_subset_with_ideology.RData")

m1 <- ols((access != "0_access_not_requested") ~ contribution * close_race +
            republican + senator + majority +
            leadership + power_committee + foreign_affairs_committee +
            registrant.id + congress,
          x = T, y = T,
          legislator_registrant_congress_data %>%
            filter(congress %in% 113:114)) %>%
  robcov(cluster = legislator_registrant_congress_data %>%
           filter(congress %in% 113:114) %>%
           pull(legislator.id))

m2 <- ols((access %in% c("2_access_obtained_to_staff",
                         "3_access_obtained_to_member")) ~
            contribution * close_race +
            republican + senator + majority +
            leadership + power_committee + foreign_affairs_committee +
            registrant.id + congress,
          x = T, y = T,
          legislator_registrant_congress_data %>%
            filter(access != "0_access_not_requested")) %>%
  robcov(cluster = legislator_registrant_congress_data %>%
           filter(access != "0_access_not_requested") %>%
           pull(legislator.id))

m3 <- ols((access %in% c("2_access_obtained_to_staff", 
                         "3_access_obtained_to_member")) ~ 
            contribution * ideological_distance +
            republican + senator + majority + leadership +
            power_committee + foreign_affairs_committee +
            number_of_clients + congress,
          x = T, y = T,
          legislator_registrant_congress_subset_with_ideology %>%
            filter(access != "0_access_not_requested")) %>%
  robcov(cluster = legislator_registrant_congress_subset_with_ideology %>%
           filter(access != "0_access_not_requested") %>%
           pull(legislator.id))

m4 <- ols((access == "3_access_obtained_to_member") ~
            contribution * ideological_distance + 
            republican + senator + majority +
            leadership + power_committee + foreign_affairs_committee +
            number_of_clients + congress,
          x = T, y = T,
          legislator_registrant_congress_subset_with_ideology %>%
            filter(access %in% c("2_access_obtained_to_staff",
                                 "3_access_obtained_to_member"))) %>%
  robcov(cluster = legislator_registrant_congress_subset_with_ideology %>%
           filter(access %in% c("2_access_obtained_to_staff",
                                "3_access_obtained_to_member")) %>%
           pull(legislator.id))

stargazer(m1, m2, m3, m4,
          dep.var.labels = c("Request for Access", "Gaining Access", "Personal Access"),
          omit = "registrant.id.*|congress.*",
          covariate.labels = c("Contribution", "Close race", "Ideological distance", "Republican", "Senator", "Majority", "Leadership", "Power committee", "Foreign affairs committee", "Number of clients", "Contribution x Close race", "Contribution x Ideological distance"),
          add.lines = list(c("Registrant (lobbyist) FE", "Y", "Y", "N", "N"),
                           c("Congress FE", "Y", "Y", "Y", "Y")),
          star.cutoffs = c(0.05), star.char = c("*"), df = F,
          notes = "*p$<$.05", notes.append = F, style = "ajps",
          type = "text")

#### Appendix D.3 (Incorporating FEC Records of Campaign Contributions) ####
rm(list = ls())
load("legislator_registrant_year_data_fec_incorporated.RData")
load("legislator_registrant_year_subset_with_ideology_fec_incorporated.RData")

# Table A-7
table(legislator_registrant_year_data$contribution_FEC,
      legislator_registrant_year_data$contribution_FARA)

# Table A-8
m1 <- ols((access != "0_access_not_requested") ~ contribution * close_race +
            republican + senator + majority +
            leadership + power_committee + foreign_affairs_committee +
            registrant.id + year,
          x = T, y = T,
          legislator_registrant_year_data %>%
            filter(year %in% 2016:2019)) %>%
  robcov(cluster = legislator_registrant_year_data %>%
           filter(year %in% 2016:2019) %>%
           pull(legislator.id))

m2 <- ols((access %in% c("2_access_obtained_to_staff",
                         "3_access_obtained_to_member")) ~
            contribution * close_race +
            republican + senator + majority +
            leadership + power_committee + foreign_affairs_committee +
            registrant.id + year,
          x = T, y = T,
          legislator_registrant_year_data %>%
            filter(access != "0_access_not_requested")) %>%
  robcov(cluster = legislator_registrant_year_data %>%
           filter(access != "0_access_not_requested") %>%
           pull(legislator.id))

m3 <- ols((access %in% c("2_access_obtained_to_staff", 
                         "3_access_obtained_to_member")) ~ 
            contribution * ideological_distance +
            republican + senator + majority + leadership +
            power_committee + foreign_affairs_committee +
            number_of_clients + year,
          x = T, y = T,
          legislator_registrant_year_subset_with_ideology %>%
            filter(access != "0_access_not_requested")) %>%
  robcov(cluster = legislator_registrant_year_subset_with_ideology %>%
           filter(access != "0_access_not_requested") %>%
           pull(legislator.id))

m4 <- ols((access == "3_access_obtained_to_member") ~
            contribution * ideological_distance + 
            republican + senator + majority +
            leadership + power_committee + foreign_affairs_committee +
            number_of_clients + year,
          x = T, y = T,
          legislator_registrant_year_subset_with_ideology %>%
            filter(access %in% c("2_access_obtained_to_staff",
                                 "3_access_obtained_to_member"))) %>%
  robcov(cluster = legislator_registrant_year_subset_with_ideology %>%
           filter(access %in% c("2_access_obtained_to_staff",
                                "3_access_obtained_to_member")) %>%
           pull(legislator.id))

stargazer(m1, m2, m3, m4,
          dep.var.labels = c("Request for Access", "Gaining Access", "Personal Access"),
          omit = "registrant.id.*|year.*",
          covariate.labels = c("Contribution", "Close race", "Ideological distance", "Republican", "Senator", "Majority", "Leadership", "Power committee", "Foreign affairs committee", "Number of clients", "Contribution x Close race", "Contribution x Ideological distance"),
          add.lines = list(c("Registrant (lobbyist) FE", "Y", "Y", "N", "N"),
                           c("Year FE", "Y", "Y", "Y", "Y")),
          star.cutoffs = c(0.05), star.char = c("*"), df = F,
          notes = "*p$<$.05", notes.append = F, style = "ajps",
          type = "text")

#### Appendix D.4 (Excluding Likely Low-Quality Access to Legislators) ####
rm(list = ls())
load("legislator_registrant_year_data_low_quality_access_excluded.RData")
load("legislator_registrant_year_subset_with_ideology_low_quality_access_excluded.RData")

m1 <- ols((access != "0_access_not_requested") ~ contribution * close_race +
            republican + senator + majority +
            leadership + power_committee + foreign_affairs_committee +
            registrant.id + year,
          x = T, y = T,
          legislator_registrant_year_data %>%
            filter(year %in% 2016:2019)) %>%
  robcov(cluster = legislator_registrant_year_data %>%
           filter(year %in% 2016:2019) %>%
           pull(legislator.id))

m2 <- ols((access %in% c("2_access_obtained_to_staff",
                         "3_access_obtained_to_member")) ~
            contribution * close_race +
            republican + senator + majority +
            leadership + power_committee + foreign_affairs_committee +
            registrant.id + year,
          x = T, y = T,
          legislator_registrant_year_data %>%
            filter(access != "0_access_not_requested")) %>%
  robcov(cluster = legislator_registrant_year_data %>%
           filter(access != "0_access_not_requested") %>%
           pull(legislator.id))

m3 <- ols((access %in% c("2_access_obtained_to_staff", 
                         "3_access_obtained_to_member")) ~ 
            contribution * ideological_distance +
            republican + senator + majority + leadership +
            power_committee + foreign_affairs_committee +
            number_of_clients + year,
          x = T, y = T,
          legislator_registrant_year_subset_with_ideology %>%
            filter(access != "0_access_not_requested")) %>%
  robcov(cluster = legislator_registrant_year_subset_with_ideology %>%
           filter(access != "0_access_not_requested") %>%
           pull(legislator.id))

m4 <- ols((access == "3_access_obtained_to_member") ~
            contribution * ideological_distance + 
            republican + senator + majority +
            leadership + power_committee + foreign_affairs_committee +
            number_of_clients + year,
          x = T, y = T,
          legislator_registrant_year_subset_with_ideology %>%
            filter(access %in% c("2_access_obtained_to_staff",
                                 "3_access_obtained_to_member"))) %>%
  robcov(cluster = legislator_registrant_year_subset_with_ideology %>%
           filter(access %in% c("2_access_obtained_to_staff",
                                "3_access_obtained_to_member")) %>%
           pull(legislator.id))

stargazer(m1, m2, m3, m4,
          dep.var.labels = c("Request for Access", "Gaining Access", "Personal Access"),
          omit = "registrant.id.*|year.*",
          covariate.labels = c("Contribution", "Close race", "Ideological distance", "Republican", "Senator", "Majority", "Leadership", "Power committee", "Foreign affairs committee", "Number of clients", "Contribution x Close race", "Contribution x Ideological distance"),
          add.lines = list(c("Registrant (lobbyist) FE", "Y", "Y", "N", "N"),
                           c("Year FE", "Y", "Y", "Y", "Y")),
          star.cutoffs = c(0.05), star.char = c("*"), df = F,
          notes = "*p$<$.05", notes.append = F, style = "ajps",
          type = "text")